const app =  getApp();
Page({
  data: {
    couponsShowed: false,
    markInputShowed: false,
    markInfo:'',
    customerShowed: false,
    commitBtnDisabled: true,
    currentAddress: app.gData.currentAddress,
    goods:[],
    totalPrice:0,
    coupons:[
      {name:'折扣券',limit:200 ,discount:9.8,selected:false,valid_date:'2024-01-12 23:59:59',disabled:false},
      {name:'季度福利卡',limit:1000,discount:9.5,selected:false,valid_date:'2024-02-12 12:00:00',disabled:false},
      {name:'周年庆折扣卡',limit:2000,discount:9.2,selected:false,valid_date:'2024-03-12 12:00:00',disabled:false},
      {name:'老会员福利卡',limit:3000,discount:9.0,selected:false,valid_date:'2024-04-12 00:00:00',disabled:false}],
    selectedCouponIndex:-1,
    selectedCouponDes:'选择优惠券',
    totalPriceAfterDiscount:0,
    openedBill:false
  },
  onLoad(option) {
    this.setData({
      commitBtnDisabled:app.gData.currentAddress==null,
      currentAddress:app.gData.currentAddress,
      goods:app.gData.goodsForCommit
    });
    //
    let totalPrice=0;
    this.data.goods.forEach(item=>totalPrice+=item.price);
    //
    let coupons=this.data.coupons;
    for(let i=0;i<coupons.length;i++){
      coupons[i].disabled=(totalPrice<coupons[i].limit);
    }
    //
    this.setData({
      totalPrice:totalPrice,totalPriceAfterDiscount:totalPrice,
      coupons:coupons
    });
    //
    this.setData({openedBill:option.opened_bill=='1'});
  },
  toAddressList(e) {
    wx.navigateTo({
      url: '/pages/usercenter/address/list/index',
    });
  },
  showCoupons(e) {
    this.setData({couponsShowed:true});
  },
  onPopClose(e) {
    this.setData({couponsShowed:false});
  },
  onCouponsCBChange(e) {
    //console.log('onCouponsCBChange:',e.detail.selected);
    let coupons=this.data.coupons;
    let selectedCouponIndex=e.detail.index;
    let selectedCouponDes='选择优惠券';
    if(e.detail.selected){
      coupons[selectedCouponIndex].selected=true;
      selectedCouponDes=coupons[selectedCouponIndex].name+' ( '+coupons[selectedCouponIndex].discount+'折 )';
      this.setData({totalPriceAfterDiscount:this.data.totalPrice*coupons[selectedCouponIndex].discount*0.1});
      if(this.data.selectedCouponIndex>-1)
        coupons[this.data.selectedCouponIndex].selected=false;
    }else{
      this.setData({totalPriceAfterDiscount:this.data.totalPrice});
      coupons[selectedCouponIndex].selected=false;
      selectedCouponIndex=-1;
    }
    this.setData({
      coupons:coupons,
      selectedCouponIndex:selectedCouponIndex,
      selectedCouponDes:selectedCouponDes});
  },
  toBill(e) {
    wx.navigateTo({url: 'bill/index'});
  },
  showMarkInput(e) {
    this.setData({markInputShowed:true});
  },
  beforeCloseTagInput(e) {
    if(e.detail.action=='cancel'){
      this.setData({markInputShowed:false});
    }else {
      if(!e.detail.value){
        wx.showToast({
          title: '请填写备注信息',
        });
        return;
      }
      //提交代码
      this.setData({markInputShowed:false});
    }
  },
  confirmMarkInput(e) {
    Toast.success('备注成功');
    this.hideMarkInput();
  },
  bindTextAreaBlur(e) {
  },
  onConfirm(e) {
    if(this.data.commitBtnDisabled){
      wx.showToast({
        title: '请选择收货地址',
      });
    }else{
      wx.navigateTo({url: '/pages/usercenter/orders/index?from=commit',});
    }
  }
})